package com.zut.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zut.dao.PostDao;
import com.zut.domain.Post;
import com.zut.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostDao postDao;


    public PageInfo<Map<String, String>> getAll(Integer pageNum, Integer pageSize) {
        List<Map<String, String>> list;
        PageHelper.startPage(pageNum, pageSize);
        list = postDao.getAll();
        return new PageInfo<>(list);
    }

    public boolean insert(Post post) {
        return postDao.insert(post) > 0;
    }

    public boolean deleteById(String id) {
        return postDao.deleteById(id) > 0;
    }

    public boolean update(Post post) {
        return postDao.update(post) > 0;
    }

    public boolean updateClick(String id) {
        return postDao.updateClick(id) > 0;
    }

    public PageInfo<Map<String, String>> getAllBySubject(Integer pageNum, Integer pageSize, String subject) {

        if (subject.equals("1")) {
            subject = "科目一";
        } else if (subject.equals("2")) {
            subject = "科目二";
        } else if (subject.equals("3")) {
            subject = "科目三";
        } else {
            subject = "科目四";
        }
        List<Map<String, String>> list;
        PageHelper.startPage(pageNum, pageSize);
        list = postDao.getAllBySubject(subject);
        return new PageInfo<>(list);
    }

    public PageInfo<Map<String, String>> getAllByTime(Integer pageNum, Integer pageSize) {
        List<Map<String, String>> list;
        PageHelper.startPage(pageNum, pageSize);
        list = postDao.getAllByTime();
        return new PageInfo<>(list);
    }

    public PageInfo<Map<String, String>> getAllByClick(Integer pageNum, Integer pageSize) {
        List<Map<String, String>> list;
        PageHelper.startPage(pageNum, pageSize);
        list = postDao.getAllByClick();
        return new PageInfo<>(list);
    }

    public Map<String, String> getById(String id) {
        Map<String, String> byId = postDao.getById(id);
        return byId;
    }

    public Map<String, String> getMessage(String id,String type) {
        if (type.equals("学员")) {
            Map<String, String> studentMessage = postDao.getStudentMessage(id);
            return studentMessage;
        } else {
            Map<String, String> coachMessage = postDao.getCoachMessage(id);
            return coachMessage;
        }
    }

    /*根据用户id获取用户发布的所有帖子*/
    public List<Map<String, String>> getPostByUserId(String id) {
        List<Map<String, String>> postByUserId = postDao.getPostByUserId(id);
        return postByUserId;
    }
}
